package com.xxl.job.admin.wisdom;

import com.wisdom.tool.sdk.client.OpenClient;
import com.wisdom.wisdomhub.api.JwtSessionInfo;
import com.wisdom.wisdomhub.opeanapi.WisdomRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.company.CompanyApiRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.job.JobApiRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.menu.MenuApiRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.organization.OrganizationApiRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.role.RoleApiRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.session.SessionApiRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.system.SystemApiRestTemplate;
import com.wisdom.wisdomhub.opeanapi.api.user.AccountUserApiRestTemplate;
import com.wisdom.wisdomhub.openapi.session.param.OpenUserLogin;
import com.xxl.job.admin.core.util.JacksonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * @Description: 对接平台组的接口调用
 * @User: zzh
 * @Date: 2021/12/20 10:19
 */
@Component
public class WisdomRemoteUtil {


    private OpenClient openClient;
    private WisdomRestTemplate wisdomRestTemplate;
    private SessionApiRestTemplate sessionApiRestTemplate;
    private AccountUserApiRestTemplate accountUserApiRestTemplate;
    private SystemApiRestTemplate systemApiRestTemplate;
    private RoleApiRestTemplate roleApiRestTemplate;
    private CompanyApiRestTemplate companyApiRestTemplate;
    //机构接口
    private OrganizationApiRestTemplate organizationRestTemplate;
    //岗位接口
    private JobApiRestTemplate jobApiRestTemplate;
    //菜单
    private MenuApiRestTemplate menuApiRestTemplate;

    private WisdomConfig wisdomConfig;

    public WisdomRemoteUtil(WisdomConfig wisdomConfig) {
        this.wisdomConfig = wisdomConfig;
        String openUrl = "http://" + wisdomConfig.getAddress() + ":" + wisdomConfig.getPort() + "/api";
        openClient = new OpenClient(openUrl, wisdomConfig.getAppkey(), wisdomConfig.getSecret());
        wisdomRestTemplate = new WisdomRestTemplate(openClient);
        sessionApiRestTemplate = new SessionApiRestTemplate(wisdomRestTemplate);
        accountUserApiRestTemplate = new AccountUserApiRestTemplate(wisdomRestTemplate);
        systemApiRestTemplate = new SystemApiRestTemplate(wisdomRestTemplate);
        roleApiRestTemplate = new RoleApiRestTemplate(wisdomRestTemplate);
        companyApiRestTemplate = new CompanyApiRestTemplate(wisdomRestTemplate);
        organizationRestTemplate=new OrganizationApiRestTemplate(wisdomRestTemplate);
        jobApiRestTemplate=new JobApiRestTemplate(wisdomRestTemplate);
        menuApiRestTemplate=new MenuApiRestTemplate(wisdomRestTemplate);
    }

    public SessionApiRestTemplate getSessionApiRestTemplate() {
        return sessionApiRestTemplate;
    }

    public AccountUserApiRestTemplate getAccountUserApiRestTemplate() {
        return accountUserApiRestTemplate;
    }

    public SystemApiRestTemplate getSystemApiRestTemplate() {
        return systemApiRestTemplate;
    }

    public CompanyApiRestTemplate getCompanyApiRestTemplate() {
        return companyApiRestTemplate;
    }

    public OrganizationApiRestTemplate getOrganizationRestTemplate() {
        return organizationRestTemplate;
    }

    public JobApiRestTemplate getJobApiRestTemplate() {
        return jobApiRestTemplate;
    }

    public MenuApiRestTemplate getMenuApiRestTemplate() {
        return menuApiRestTemplate;
    }

    public RoleApiRestTemplate getRoleApiRestTemplate() {
        return roleApiRestTemplate;
    }

    public JwtSessionInfo login(OpenUserLogin openUserLogin) {
        return sessionApiRestTemplate.login(openUserLogin);
    }

    /**
     * 获取request
     *
     * @param
     * @return javax.servlet.http.HttpServletRequest
     * @throws
     * @author Luo
     * @modifior Luo
     * @version 0.01
     * @Date 2021/8/20 11:08
     */
    public HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        return ((ServletRequestAttributes) requestAttributes).getRequest();
    }

    /**
     * 获取Response
     *
     * @param
     * @return javax.servlet.http.HttpServletRequest
     * @throws
     * @author Luo
     * @modifior Luo
     * @version 0.01
     * @Date 2021/8/20 11:08
     */
    public HttpServletResponse getResponse() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        return ((ServletRequestAttributes) requestAttributes).getResponse();
    }

    public String getToken() {
        String token = getRequest().getHeader("token");
        if (StringUtils.isBlank(token)) {
            token = getRequest().getParameter("token");
        }
        return StringUtils.isNotBlank(token) ? token : "";
    }

    public String makeToken(JwtSessionInfo jwtSessionInfo){
        String tokenJson = JacksonUtil.writeValueAsString(jwtSessionInfo);
        String tokenHex = new BigInteger(tokenJson.getBytes()).toString(16);
        return tokenHex;
    }

    public JwtSessionInfo parseToken(String tokenHex){
        JwtSessionInfo xxlJobUser = null;
        if (tokenHex != null) {
            String tokenJson = new String(new BigInteger(tokenHex, 16).toByteArray());      // username_password(md5)
            xxlJobUser = JacksonUtil.readValue(tokenJson, JwtSessionInfo.class);
        }
        return xxlJobUser;
    }

}
